<?php

namespace App\Store\Models\Sysset;

use App\Common\Services\Common;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use App\Common\Services\Store\Common as StoreCommon;

class LogLogin extends Model
{
    protected $table = 'site_sysset_log_login';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];

    /**
     *获取列表 
     */
    public static function getList(Request $request)
    {

        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        $query = self::where(['cp_id' => $cp_id,'is_del'=>1])->orderBy('login_at', 'desc');
        $account_name = strval($request->account_name);
        if ($account_name!='') {
            $query->where('account_name', 'like','%'.$account_name.'%');
        }

        $account_id = intval($request->account_id);
        if ($account_id>0) {
            $query->where('account_id', $account_id);
        }

        $start_time = strtotime($request->start_time) > 0 ? strtotime($request->start_time) : intval($request->start_time);
        $end_time = strtotime($request->end_time) > 0 ? strtotime($request->end_time) : intval($request->end_time);
        if ($start_time > 0 && $end_time > $start_time) {
            $query->where('login_at', '>=', $start_time)->where('login_at', '<=', $end_time);
        }
        $limit = $request->limit ?? 10;
        $list = $query->paginate($limit)->toArray();
        return Common::paginateDataToArray($list);
    }
}
